<script>
import { ref } from "vue";
import { useStore } from "vuex";
import $ from "jquery";

export default {
    setup() {
        const store = useStore();

        let bots = ref([]);
        let select_bot = ref("-1");
        let match_btn_info = ref("开始匹配");

        const click_match_btn = () => {
            if (match_btn_info.value === "开始匹配") {
                match_btn_info.value = "取消匹配";
                store.state.pk.socket.send(
                    JSON.stringify({
                        event: "start-matching",
                        bot_id: select_bot.value,
                    })
                );
            } else {
                match_btn_info.value = "开始匹配";
                store.state.pk.socket.send(
                    JSON.stringify({
                        event: "stop-matching",
                    })
                );
            }
        };

        const refresh_bots = () => {
            $.ajax({
                url: "http://127.0.0.1:3000/user/bot/getlist",
                type: "get",
                headers: {
                    Authorization: "Bearer " + store.state.user.token,
                },
                success(resp) {
                    bots.value = resp;
                },
            });
        };

        refresh_bots();

        return {
            match_btn_info,
            click_match_btn,
            bots,
            select_bot,
        };
    },
};
</script>
<template>
    <div class="matchground d-flex align-items-center justify-content-center">
        <div class="row w-100 text-center">
            <div class="col-4 d-flex flex-column align-items-center justify-content-center">
                <div class="user-photo">
                    <img :src="$store.state.user.photo" alt="User Photo" />
                </div>
                <div class="user-name">{{ $store.state.user.username }}</div>
            </div>

            <div class="col-4 d-flex flex-column align-items-center justify-content-center">
                <div class="user-select-bot">
                    <select v-model="select_bot" class="form-select large-select" aria-label="Select Bot">
                        <option value="-1" selected>亲自上阵</option>
                        <option v-for="bot in bots" :key="bot.id" :value="bot.id">
                            {{ bot.title }}
                        </option>
                    </select>
                </div>
            </div>

            <div class="col-4 d-flex flex-column align-items-center justify-content-center">
                <div class="user-photo">
                    <img :src="$store.state.pk.opponent_photo" alt="Opponent Photo" />
                </div>
                <div class="user-name">{{ $store.state.pk.opponent_name }}</div>
            </div>

            <div class="col-12 mt-5">
                <button @click="click_match_btn" type="button" class="btn btn-warning btn-lg">
                    {{ match_btn_info }}
                </button>
            </div>
        </div>
    </div>
</template>

<style scoped>
.matchground {
    width: 60vw;
    height: 70vh;
    margin: 40px auto;
    background-color: rgba(50, 50, 50, 0.7);
    border-radius: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.user-photo {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}

.user-photo img {
    border-radius: 50%;
    width: 15vh;
    border: 3px solid #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.user-name {
    font-size: 22px;
    font-weight: 600;
    color: #fff;
    margin-top: 15px;
    text-align: center;
}

.user-select-bot {
    width: 100%;
    display: flex;
    justify-content: center;
}

.large-select {
    width: 80%;
    //max-width: 400px; /* 最大宽度 */
    padding: 15px;
    font-size: 20px;
    border-radius: 12px;
    border: none;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    background-color: #f0f0f0;
    color: #333;
}

.btn {
    padding: 15px 40px;
    font-size: 22px;
    border-radius: 50px;
    transition: background-color 0.3s ease;
}

.btn:hover {
    background-color: #e68a00;
}
</style>
